import { omit } from "lodash-es";
import mitt from "mitt";
import { defineStore } from "pinia";
import { Toast } from "vant";
import { PixelFile } from "../../components/canvas/Data/CanvasFile";
import { files, preview, user } from "./local";
import { ProPixelFile } from "./ProPixel/ProPixelFile";
import { ProPixelPreview } from "./ProPixel/ProPixelPreview";
import { ProPixelUser } from "./ProPixel/ProPixelUser";
export const FileHub = mitt<{
    saveFile: PixelFile;
}>();

export const useFileStore = defineStore("fileStore", {
    state: () => {
        return {
            /** 缓存一下被打开的文件 */
            files: {} as { [key: string]: PixelFile },
            /** 记录用户的信息 */
            user: null as ProPixelUser | null,
            /** 记录文件的缩略信息 */
            preview: [] as ProPixelPreview[],
            ready: false,
            openingFile: null as null | PixelFile,
        };
    },
    getters: {},
    actions: {
        /** 根据输入创建一个初始文件 */
        async createFile(name: string, height: number, width: number) {
            if (this.user) {
                /** 创建文件 */
                const newFile = new ProPixelFile({
                    name,
                    user: this.user.userID,
                    height,
                    width,
                } as any);
                await files.setItem(newFile.id, newFile);
                this.files[newFile.id] = newFile;
                // 创建文件的预览
                const preview = new ProPixelPreview(newFile);
                this.preview.unshift(preview);
                preview.save();
                // 保存用户
                this.user.fileList.unshift(newFile.id);
                this.user.save();
                console.log(newFile);
            }
        },
        async deleteFile(key: string) {
            const index = this.user!.fileList.indexOf(key);
            this.user!.fileList.splice(index, 1);

            await this.user!.save();
            await files.removeItem(key);
            await preview.removeItem(key);

            delete this.files[key];
            const index1 = this.preview.findIndex(({ id }) => key === id);
            this.preview.splice(index1, 1);
        },
        /** 获取一个文件的详细参数 */
        async openFile(key: string) {
            const file = await files.getItem<ProPixelFile>(key);
            if (file) {
                this.files[file.id] = file;
                this.openingFile = file;
            } else {
                Toast("打开了不存在的文件");
            }
        },
        async saveFile(file: PixelFile, image = "") {
            const data = await files.setItem(file.id, file);
            this.files[data.id] = data;

            const tempData = await preview.setItem(file.id, {
                ...omit(file, "layers"),
                image,
            });
            const index = this.preview.findIndex((i) => i.id == file.id);
            this.preview.splice(index, 1, new ProPixelPreview(tempData));
        },

        async loadPreview() {
            if (this.user) {
                const fileList = this.user.fileList;
                const promises = fileList.map(async (previewKey) => {
                    const data = await preview.getItem<ProPixelPreview>(
                        previewKey
                    );
                    return new ProPixelPreview(data);
                });
                this.preview = await Promise.all(promises);
            }
        },

        /** 登录本地账户, 需要先登录一个用户才行 */
        async loginUser() {
            if (!this.user) {
                const User = await user.getItem<ProPixelUser>("userMessage");
                this.user = new ProPixelUser(User);
                return true;
            }
            return false;
        },
    },
});
